package br.edu.ufcg.splab.techniques.reduction.requirement;

import java.util.HashSet;
import java.util.Set;

import br.edu.ufcg.splab.core.InterfaceEdge;
import br.edu.ufcg.splab.generator.TestCase;
import br.edu.ufcg.splab.generator.TestSuite;

public class TransitionsTracerGenerator implements InterfaceRequirementTracerGenerator {
	
	/**
	 * This method have the role of extract the transitions (requirements) from the test suite passed as 
	 * parameter.  
	 * @param root The root of the model that the suite was extracted.
	 * @param suite The suite to be related with the requirements.
	 * @return The tracer of the requirements to the test cases that satisfy them.
	 */	
	public RequirementTracer getTracer(TestSuite suite) {
		RequirementTracer tracer = new RequirementTracer();		
		for(String pair: getEdges(suite)) {
			InterfaceRequirement req = new Requirement();
			req.setLabel(pair);
			for (TestCase test : suite.getTestSuite()) {
				if(containsEdges(test, pair)){
					tracer.addTestCaseAndReq(test, req);
				}
			}
			tracer.addRequirement(req);
		}
		tracer.setTypeRequirement(this.getTypeRequirementTracer());
		return tracer;
	}
	
	public String getTypeRequirementTracer(){
		return "all-transitions";
	}
			
	/**
	 * Provide the transitions of the graph.
	 * 
	 * @param testSuite The test suite
	 * @param root The root of the graph measured.
	 * @return Returns all transitions of the graph.
	 */
	private Set<String> getEdges(TestSuite testSuite){
		Set<String> str = new HashSet<String>();
		for(TestCase tc : testSuite.getTestSuite()){
			str.addAll(this.getEdges(tc));
		}
		return str;
	}
	
	/**
	 * Returns <tt>true</tt> if the test case contains the specified transition. 
	 * 
	 * @param testCase The test case for evaluation.
	 * @param transition The transition whose presence in this test case is to be tested
	 * @return <tt>true</tt> if this list contains the specified transition. 
	 */
	private boolean containsEdges(TestCase testCase, String transition) {	
		for(String str: getEdges(testCase)){
			if(str.equals(transition)){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Returns the list of the transitions of the test case.
     *
	 * @param testCase The test case for evaluation.
	 * @return The set of the transitions.
	 */
	private Set<String> getEdges(TestCase testCase){
		Set<String> transitions = new HashSet<String>();		
		String s1 = "";
		for(InterfaceEdge edge: testCase.getTestCase()){
			s1 = edge.getLabel();
			transitions.add("(\"" + s1 + "\")");
		}
		return transitions;
	}
	
}
